<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta name="generator" content="Hugo 0.82.1" />
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Go For 语句#For 语句 Range#   Range expression 1st value 2nd value     array or slice a [n]E, *[n]E, or []E index i int a[i] E   string s string type index i int see below rune   map m map[K]V key k K m[k] V   channel c chan E, &lt;-chan E element e E     var testdata *struct { a *[7]int } for i, _ := range testdata.">
<meta name="theme-color" content="#FFFFFF"><meta property="og:title" content="Go For语句" />
<meta property="og:description" content="Go For 语句#For 语句 Range#   Range expression 1st value 2nd value     array or slice a [n]E, *[n]E, or []E index i int a[i] E   string s string type index i int see below rune   map m map[K]V key k K m[k] V   channel c chan E, &lt;-chan E element e E     var testdata *struct { a *[7]int } for i, _ := range testdata." />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://example.org/docs/statements/" /><meta property="article:section" content="docs" />



<title>Go For语句 | My Golang Book</title>
<link rel="manifest" href="/manifest.json">
<link rel="icon" href="/favicon.png" type="image/x-icon">
<link rel="stylesheet" href="/book.min.2bc2364a4a4b31f0ec1debecf0a8f90a840821f143dfe1ac6a0f7cbcbdcf64ac.css" integrity="sha256-K8I2SkpLMfDsHevs8Kj5CoQIIfFD3&#43;Gsag98vL3PZKw=">
<script defer src="/en.search.min.84ce9693e6c02d95376f29aeb794c11824d09ed0440f910f008e91e9ed0436e4.js" integrity="sha256-hM6Wk&#43;bALZU3bymut5TBGCTQntBED5EPAI6R6e0ENuQ="></script>
<!--
Made with Book Theme
https://github.com/alex-shpak/hugo-book
-->

  
</head>

<body dir="ltr">
  <input type="checkbox" class="hidden toggle" id="menu-control" />
  <input type="checkbox" class="hidden toggle" id="toc-control" />
  <main class="container flex">
    <aside class="book-menu">
      <div class="book-menu-content">
        
  <nav>
<h2 class="book-brand">
  <a href="/"><span>My Golang Book</span>
  </a>
</h2>


<div class="book-search">
  <input type="text" id="book-search-input" placeholder="Search" aria-label="Search" maxlength="64" data-hotkeys="s/" />
  <div class="book-search-spinner hidden"></div>
  <ul id="book-search-results"></ul>
</div>











  



  
  <ul>
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/introduction/" class="">Golang 介绍</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/installation/" class="">安装、准备开发环境</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/go-routine/" class="">并发处理 - Go 协程</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/workspace-module/" class="">管理Go模块</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/erro-handling/" class="">错误处理</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/resources/" class="">Golang 资源</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/statements/" class=" active">Go For语句</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/guidelines/" class="">Go 开发规范</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/logging/" class="">日志</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/data-init/" class="">Data Init</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/data-types/" class="">Data Types</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/debug/" class="">Debug</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/dynamic-programing/" class="">Dynamic Programing</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/ecosystem/" class="">Ecosystem</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/performance/" class="">Performance</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/struct-interface/" class="">Struct Interface</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/struct-tags/" class="">Struct Tags</a>
  

        </li>
      
    
  </ul>















</nav>




  <script>(function(){var a=document.querySelector("aside.book-menu nav");addEventListener("beforeunload",function(b){localStorage.setItem("menu.scrollTop",a.scrollTop)}),a.scrollTop=localStorage.getItem("menu.scrollTop")})()</script>


 
      </div>
    </aside>

    <div class="book-page">
      <header class="book-header">
        
  <div class="flex align-center justify-between">
  <label for="menu-control">
    <img src="/svg/menu.svg" class="book-icon" alt="Menu" />
  </label>

  <strong>Go For语句</strong>

  <label for="toc-control">
    
    <img src="/svg/toc.svg" class="book-icon" alt="Table of Contents" />
    
  </label>
</div>


  
  <aside class="hidden clearfix">
    
  
<nav id="TableOfContents">
  <ul>
    <li><a href="#for-语句-range">For 语句 Range</a></li>
    <li><a href="#参照">参照</a></li>
  </ul>
</nav>



  </aside>
  
 
      </header>

      
      
  <article class="markdown"><h1 id="go-for-语句">
  Go For 语句
  <a class="anchor" href="#go-for-%e8%af%ad%e5%8f%a5">#</a>
</h1>
<h2 id="for-语句-range">
  For 语句 Range
  <a class="anchor" href="#for-%e8%af%ad%e5%8f%a5-range">#</a>
</h2>
<table>
<thead>
<tr>
<th>Range</th>
<th>expression</th>
<th>1st value</th>
<th>2nd value</th>
</tr>
</thead>
<tbody>
<tr>
<td>array or slice</td>
<td>a [n]E, *[n]E, or []E</td>
<td>index i int</td>
<td>a[i] E</td>
</tr>
<tr>
<td>string</td>
<td>s string type</td>
<td>index i int</td>
<td>see below rune</td>
</tr>
<tr>
<td>map</td>
<td>m map[K]V</td>
<td>key k K</td>
<td>m[k] V</td>
</tr>
<tr>
<td>channel</td>
<td>c chan E, &lt;-chan E</td>
<td>element e E</td>
<td></td>
</tr>
</tbody>
</table>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#66d9ef">var</span> <span style="color:#a6e22e">testdata</span> <span style="color:#f92672">*</span><span style="color:#66d9ef">struct</span> {
	<span style="color:#a6e22e">a</span> <span style="color:#f92672">*</span>[<span style="color:#ae81ff">7</span>]<span style="color:#66d9ef">int</span>
}
<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">i</span>, <span style="color:#a6e22e">_</span> <span style="color:#f92672">:=</span> <span style="color:#66d9ef">range</span> <span style="color:#a6e22e">testdata</span>.<span style="color:#a6e22e">a</span> {
	<span style="color:#75715e">// testdata.a is never evaluated; len(testdata.a) is constant
</span><span style="color:#75715e"></span>	<span style="color:#75715e">// i ranges from 0 to 6
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">f</span>(<span style="color:#a6e22e">i</span>)
}

<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">a</span> [<span style="color:#ae81ff">10</span>]<span style="color:#66d9ef">string</span>
<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">i</span>, <span style="color:#a6e22e">s</span> <span style="color:#f92672">:=</span> <span style="color:#66d9ef">range</span> <span style="color:#a6e22e">a</span> {
	<span style="color:#75715e">// type of i is int
</span><span style="color:#75715e"></span>	<span style="color:#75715e">// type of s is string
</span><span style="color:#75715e"></span>	<span style="color:#75715e">// s == a[i]
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">g</span>(<span style="color:#a6e22e">i</span>, <span style="color:#a6e22e">s</span>)
}

<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">key</span> <span style="color:#66d9ef">string</span>
<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">val</span> <span style="color:#66d9ef">interface</span>{}  <span style="color:#75715e">// element type of m is assignable to val
</span><span style="color:#75715e"></span><span style="color:#a6e22e">m</span> <span style="color:#f92672">:=</span> <span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">int</span>{<span style="color:#e6db74">&#34;mon&#34;</span>:<span style="color:#ae81ff">0</span>, <span style="color:#e6db74">&#34;tue&#34;</span>:<span style="color:#ae81ff">1</span>, <span style="color:#e6db74">&#34;wed&#34;</span>:<span style="color:#ae81ff">2</span>, <span style="color:#e6db74">&#34;thu&#34;</span>:<span style="color:#ae81ff">3</span>, <span style="color:#e6db74">&#34;fri&#34;</span>:<span style="color:#ae81ff">4</span>, <span style="color:#e6db74">&#34;sat&#34;</span>:<span style="color:#ae81ff">5</span>, <span style="color:#e6db74">&#34;sun&#34;</span>:<span style="color:#ae81ff">6</span>}
<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">key</span>, <span style="color:#a6e22e">val</span> = <span style="color:#66d9ef">range</span> <span style="color:#a6e22e">m</span> {
	<span style="color:#a6e22e">h</span>(<span style="color:#a6e22e">key</span>, <span style="color:#a6e22e">val</span>)
}
<span style="color:#75715e">// key == last map key encountered in iteration
</span><span style="color:#75715e">// val == map[key]
</span><span style="color:#75715e"></span>
<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">ch</span> <span style="color:#66d9ef">chan</span> <span style="color:#a6e22e">Work</span> = <span style="color:#a6e22e">producer</span>()
<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">w</span> <span style="color:#f92672">:=</span> <span style="color:#66d9ef">range</span> <span style="color:#a6e22e">ch</span> {
	<span style="color:#a6e22e">doWork</span>(<span style="color:#a6e22e">w</span>)
}

<span style="color:#75715e">// empty a channel
</span><span style="color:#75715e"></span><span style="color:#66d9ef">for</span> <span style="color:#66d9ef">range</span> <span style="color:#a6e22e">ch</span> {}
</code></pre></div><h2 id="参照">
  参照
  <a class="anchor" href="#%e5%8f%82%e7%85%a7">#</a>
</h2>
<ul>
<li><a href="https://golang.google.cn/ref/spec#For_statements">For Range</a></li>
</ul>
</article>
 
      

      <footer class="book-footer">
        
  <div class="flex flex-wrap justify-between">





</div>

 
        
      </footer>

      
  
  <div class="book-comments">

</div>
  
 

      <label for="menu-control" class="hidden book-menu-overlay"></label>
    </div>

    
    <aside class="book-toc">
      <div class="book-toc-content">
        
  
<nav id="TableOfContents">
  <ul>
    <li><a href="#for-语句-range">For 语句 Range</a></li>
    <li><a href="#参照">参照</a></li>
  </ul>
</nav>


 
      </div>
    </aside>
    
  </main>

  
</body>

</html>












